﻿package jobschedule;

/*
 License for Java 1.5 'Tiger': A Developer's Notebook
 (O'Reilly) example package

 Java 1.5 'Tiger': A Developer's Notebook (O'Reilly) 
 by Brett McLaughlin and David Flanagan.
 ISBN: 0-596-00738-8

 You can use the examples and the source code any way you want, but
 please include a reference to where it comes from if you use it in
 your own products or services. Also note that this software is
 provided by the author "as is", with no expressed or implied warranties. 
 In no event shall the author be liable for any direct or indirect
 damages arising in any way out of the use of this software.
 */

import static java.util.concurrent.TimeUnit.SECONDS;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;

public class ScheduleTester {

    public static void main(String[] args) {
        // Get the scheduler
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10);

        //수행주기를 관리하는 핸들러 반환
        // Get a handle, starting now, with a 10 second delay
        final ScheduledFuture<?> timeHandle = scheduler.scheduleWithFixedDelay(
                new Testjob002(), 0, 2, SECONDS);

        // Get a handle, starting now, with a 1 second delay
        final ScheduledFuture<?> timeHandle2 = scheduler.scheduleAtFixedRate(
                new Testjob001(), 0, 1, SECONDS);
        
        
        //종료 로직
        // Schedule the event, and run for 1 hour (2 * 60 seconds)
        scheduler.schedule(new Runnable() {
            public void run() {
                timeHandle.cancel(false);
            }
        }, 2 * 60, SECONDS);

        // Schedule the event, and run for 1 hour (1 * 60 seconds)
        scheduler.schedule(new Runnable() {
            public void run() {
                timeHandle2.cancel(false);
            }
        }, 1 * 60, SECONDS);
    }
}